﻿using CollegeApp.Data.IDataAdapters.Interfaces;
using CollegeApp.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CollegeApp.Data.IDataAdapters.Adapters
{
    public class CollegeAdapter : ICollegeAdapter
    {
        public College AddCollege(College college)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Colleges.Add(college);
                db.SaveChanges();
            }
            return college;
        }

        public List<College> GetAllColleges()
        {
            List<College> colleges;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                colleges = db.Colleges.OrderBy(x => x.Name).ToList();
            }
            return colleges;
        }

        public College GetCollege(int id)
        {
            College college;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                college = db.Colleges.Find(id);
            }
            return college;
        }

        public College UpdateCollege(College college)
        {
            
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                College oldcollege;
                oldcollege = db.Colleges.Find(college.Id);
                college = oldcollege;
                db.SaveChanges();
            }
            return college;
        }

        public void DeleteCollege(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                College college = db.Colleges.Find(id);
                db.Colleges.Remove(college);
                db.SaveChanges();
            }
        }
        public List<Review> GetReviewsForCollege(int id)
        {
            List<Review> reviews;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                College college = db.Colleges.Include("Reviews").Where(x => x.Id == id).FirstOrDefault();
                reviews = college.Reviews;
            }
            return reviews;
        }
    }
}
